package com.nsyue.auth.controller;

import com.nsyue.auth.controller.result.Result;
import com.nsyue.auth.domain.granter.PlatformAuthenticationToken;
import com.nsyue.auth.dto.SyncLoginDTO;
import com.nsyue.auth.dto.SyncRegisterDTO;
import com.nsyue.auth.service.UserService;
import com.nsyue.auth.controller.verify.VerifySign;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.endpoint.CheckTokenEndpoint;
import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "OAuth接口")
@RestController
@Slf4j
public class TokenController {

    //令牌请求的端点
    @Autowired
    private TokenEndpoint tokenEndpoint;

    @Autowired
    private CheckTokenEndpoint checkTokenEndpoint;

    @Autowired
    private UserService userService;

    @Autowired
    private ClientDetailsService clientDetailsService;
    /**
     * 重写/oauth/token这个默认接口，返回的数据格式统一
     */
    @PostMapping(value = "/oauth/token")
    public Result<OAuth2AccessToken> postAccessToken(Principal principal, @RequestParam
            Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
        OAuth2AccessToken accessToken = tokenEndpoint.postAccessToken(principal, parameters).getBody();
        return Result.ok(accessToken);
    }

    /**
     * 重写/oauth/check_token这个默认接口，用于校验令牌，返回的数据格式统一
     */
    @PostMapping(value = "/oauth/check_token")
    public Result<Map<String, ?>> checkToken(@RequestParam("token") String value) {
        Map<String, ?> map = checkTokenEndpoint.checkToken(value);
        return Result.ok(map);
    }

    /**
     * 同步注册接口
     */
    @PostMapping(value = "/sso/register_sync")
    public Result<?> registerSync(@RequestBody List<SyncRegisterDTO> registerDTOList) throws HttpRequestMethodNotSupportedException {
        String register_code = userService.registerBatch(registerDTOList);
        return Result.ok(register_code);
    }

    /**
     * 自定义登录接口
     * http://localhost:8081/oauth/token?client_id=R8T3N8&client_secret=unity&grant_type=platform&platformId=13479497466
     */
    @VerifySign
    @PostMapping(value = "/sso/login_sync")
    public Result<OAuth2AccessToken> loginSync(@RequestBody SyncLoginDTO syncLoginDTO) throws HttpRequestMethodNotSupportedException {
        // 使用自定义模式获取token，省略client_secret参数，自定义principal
        Map<String, String> parameters = new HashMap<>();
        parameters.put("grant_type", "platform");
        parameters.put("client_id", syncLoginDTO.getAppid());
        parameters.put("platformId", syncLoginDTO.getMobile());
        ClientDetails clientDetails = clientDetailsService.loadClientByClientId(syncLoginDTO.getAppid());
        User user = new User(syncLoginDTO.getAppid(), "", clientDetails.getAuthorities());
        PlatformAuthenticationToken principal = new PlatformAuthenticationToken(user, clientDetails.getAuthorities());
        OAuth2AccessToken accessToken = tokenEndpoint.postAccessToken(principal, parameters).getBody();
        return Result.ok(accessToken);
    }
}